package com.hmkj.core.service.account.impl;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.account.AccountMapper;
import com.hmkj.core.model.account.AccountModel;
import com.hmkj.core.po.account.Account;
import com.hmkj.core.po.account.AccountLog;
import com.hmkj.core.service.account.AccountLogService;
import com.hmkj.core.service.account.AccountService;
import com.hmkj.core.service.cache.RedisService;
import com.xiaoleilu.hutool.util.BeanUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 用户账户表业务相关Service接口实现<br>
 *
 * @author lizhijie
 * @ClassName: AccountServiceImpl
 * @date 2018-07-05 06:18:03
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private AccountLogService accountLogService;

    @Override
    public GenericMapper<Account, Integer> _getMapper() {
        return accountMapper;
    }

    @Override
    public AccountModel getAccountModel(Integer userId) {
        Account account = accountMapper.selectOne(new Account(t -> {
            t.setUserId(userId);
            t.setDelFlag(GenericPo.DELFLAG.NORMAL.code);
        }));
        if (account == null) throw new BussinessException("查询用户账户信息失败");

        AccountModel model = new AccountModel();
        BeanUtil.copyProperties(account, model);

        BigDecimal rechargeMin = redisService.get(ConfigNID.RECHARGE_MIN, BigDecimal.class);
        if (rechargeMin == null) rechargeMin = new BigDecimal(10);// 默认单笔充值最低金额为10元
        model.setRechargeMin(rechargeMin);

        BigDecimal rechargeMax = redisService.get(ConfigNID.RECHARGE_MAX, BigDecimal.class);
        if (rechargeMax == null) rechargeMax = new BigDecimal(10000000);// 默认单笔充值最高金额为10000000元
        model.setRechargeMax(rechargeMax);

        BigDecimal cashMin = redisService.get(ConfigNID.CASH_MIN, BigDecimal.class);
        if (cashMin == null) cashMin = new BigDecimal(10);// 默认单笔提现最低金额为10元
        model.setCashMin(cashMin);

        BigDecimal cashMax = redisService.get(ConfigNID.CASH_MAX, BigDecimal.class);
        if (cashMax == null) cashMax = new BigDecimal(50000);// 默认单笔提现最高金额为50000元
        model.setCashMax(cashMax);

        BigDecimal cashFeeMin = redisService.get(ConfigNID.CASH_FEE_MIN, BigDecimal.class);
        if (cashFeeMin == null) cashFeeMin = new BigDecimal(0);// 默认单笔提现最低手续费为0元
        model.setCashFeeMin(cashFeeMin);

        BigDecimal cashFeeRatio = redisService.get(ConfigNID.CASH_FEE_RATIO, BigDecimal.class);
        if (cashFeeRatio == null) cashFeeRatio = new BigDecimal(0);// 默认提现手续费率为 0.0%
        model.setCashFeeRatio(cashFeeRatio);

        return model;

    }

    @Override
    public Account selectByUID(Integer userId) {
        return accountMapper.selectByUID(userId);
    }

    @Override
    public Account getPlatAccount() {
        return accountMapper.selectByUID(1);
    }

    @Override
    public void updateAccount(Integer userId, BigDecimal amount, Integer type) {
        Account account = selectByPrimaryKey(userId);
        if (type == 1) {
            if (account.getAvailable().compareTo(amount) != -1) {
                AccountLog accountLog = AccountLog.buildLog(userId, 0, amount, AccountLog.TYPE.T27, account, "转到钱包余额");
                if (accountLogService.insert(accountLog) != 1) {
                    throw new BussinessException("策略余额转到钱包余额失败");
                }
                if (accountMapper.updateAccount(BigDecimal.ZERO, amount, amount.negate(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, userId) != 1) {
                    throw new BussinessException("策略余额转到钱包余额失败");
                }
            } else {
                throw new BussinessException("策略余额不足");
            }
        } else if (type == 2) {
            if (account.getMoneyAvailable().compareTo(amount) != -1) {
                AccountLog accountLog = AccountLog.buildLog(userId, 0, amount, AccountLog.TYPE.T26, account, "转到策略余额");
                if(accountLogService.insert(accountLog) != 1){
                    throw new BussinessException("钱包余额转到策略余额失败");
                }
                if(accountMapper.updateAccount(BigDecimal.ZERO, amount.negate(), amount, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, userId) !=1){
                    throw new BussinessException("钱包余额转到策略余额失败");
                }
            } else {
                throw new BussinessException("钱包余额不足");
            }
        } else {
            throw new BussinessException("出入金额类型错误!");
        }
    }
}
